<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $allowedTypes = array('image/jpeg', 'image/png', 'image/gif');
    $allowedSize = 2 * 1024 * 1024; // 2MB
    if (in_array($_FILES['file']['type'], $allowedTypes) && $_FILES['file']['size'] <= $allowedSize) {
        $uploadDir = 'uploads/';
        if (!file_exists($uploadDir)) {
            mkdir($uploadDir);
        }
        $uploadFile = $uploadDir . basename($_FILES['file']['name']);
        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
            echo '上传成功：' . $uploadFile;
        } else {
            echo '上传失败';
        }
    } else {
        echo '文件格式或大小不符合要求';
    }
}